package com.tencent.web.controller;

import com.github.pagehelper.PageInfo;
import com.tencent.constant.MessageConstant;
import com.tencent.entity.Member;
import com.tencent.entity.Pagination;
import com.tencent.entity.Result;
import com.tencent.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/member1")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @GetMapping
    public Result findAll(Pagination pagination){
        PageInfo<Member> byName = memberService.findByName(pagination);
//        Result result = new Result(true, "ok", byName);
        return Result.success(byName);
    }
    //新增会员档案
    @RequestMapping ("/add")
    public Result add( @RequestBody Member member){
        try {
            memberService.add(member);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败了
            return Result.error(MessageConstant.ADD_MEMBER_FAIL);
        }
        return Result.success(MessageConstant.ADD_MEMBER_SUCCESS);
    }
//编辑会员档案
    @RequestMapping ("/edit")
    public Result edit( @RequestBody Member member){
        try {
            memberService.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败了
            return Result.error(MessageConstant.EDIT_MEMBER_FAIL);
        }
        return Result.success(MessageConstant.EDIT_MEMBER_SUCCESS);
    }
    @RequestMapping ("/findById")
    public Result findById(Integer id){
        try {
            Member member = memberService.findById(id);
            return Result.success(member);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败了
            return Result.error(MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }
    //删除
    @RequestMapping ("/delete")
    public Result delete(Integer id){
        try {
            memberService.deleteById(id);

        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败了
            return Result.error(MessageConstant.DELETE_MEMBER_FAIL);
        }
        return Result.success(MessageConstant.DELETE_MEMBER_SUCCESS);
    }
}
